package com.example.datn.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.example.datn.objects.SongObject;

public class RequestManager {
	
	public ArrayList<SongObject> loadListSong(String url){
		ArrayList<SongObject> list = new ArrayList<SongObject>();
		String songName = "", urlSong = "", albumName = "";
		int luotNghe = 0, id = 0;
		SongObject song;
		/************ Make Post Call To Web Server ***********/
		Document doc;
		try {
			doc = Jsoup.connect("http://mp3.zing.vn/nghe-si/Quang-Le/bai-hat").get();			
			Elements links = doc.select("ul");
			for (Element link : links) {				 
				if(link.hasClass("song-list")){				
					Elements divs = link.select("li");
					for (Element div : divs) {
						Elements as = div.select("a");
						for (Element a : as) {
							if(a.hasClass("_strCut")){
								if(a.attr("strlength").equals("35")){
									songName = a.text();
									urlSong = "http://mp3.zing.vn" + a.attr("href");										
								}									
								else
									albumName = a.text();
							}
							if(a.hasClass("ln-bar")){								
								Pattern p = Pattern.compile("[0-9]+");
								Matcher m = p.matcher(a.attr("title"));
								if (m.find()) {
								    luotNghe = Integer.parseInt(m.group());									    
								}
							}													
						}
						song = new SongObject();
						song.songNo = id;
						song.songName = songName;
						song.urlSong = urlSong;
						song.albumName = albumName;
						list.add(song);
					}
					break;
				}	
			}
			System.out.println("\n============================================");
		} catch (IOException e) {		
			e.printStackTrace();
		}
		
		return list;
	}
}
